package com.sfx.shortLink.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sfx.shortLink.dao.entity.LinkBrowserStatsDO;
import com.sfx.shortLink.dto.req.ShortLinkGroupStatsReqDTO;
import com.sfx.shortLink.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ILinkBrowserMapper extends BaseMapper<LinkBrowserStatsDO> {


    /**
     * 记录浏览器访问监控数据
     */
    @Insert("INSERT INTO t_link_browser_stats(full_short_url, gid, date, cnt, browser, create_time, update_time, del_flag) " +
            "VALUES( #{linkBrowserStats.fullShortUrl}, #{linkBrowserStats.gid}, #{linkBrowserStats.date}, #{linkBrowserStats.cnt}, #{linkBrowserStats.browser}, NOW(), NOW(), 0) " +
            "ON DUPLICATE KEY UPDATE cnt = cnt +  #{linkBrowserStats.cnt};")
    void browserStats(@Param("linkBrowserStats") LinkBrowserStatsDO linkBrowserStats);


    @Select("SELECT browser,SUM(cnt) as cnt " +
            "FROM t_link_browser_stats " +
            "WHERE full_short_url = #{param.fullShortUrl} AND gid = #{param.gid} AND date between #{param.startDate} AND #{param.endDate} " +
            "group by full_short_url,gid,browser;"
    )
    List<LinkBrowserStatsDO> listBrowserStatsByShortLink(@Param("param") ShortLinkStatsReqDTO param);

    @Select("SELECT browser,SUM(cnt) as cnt " +
            "FROM t_link_browser_stats " +
            "WHERE  gid = #{param.gid} AND date between #{param.startDate} AND #{param.endDate} " +
            "group by gid,browser;"
    )
    List<LinkBrowserStatsDO> listBrowserStatsByShortLinkGroup(@Param("param") ShortLinkGroupStatsReqDTO param);
}
